package com.blog.mapper;

import com.blog.pojo.Article;

import com.blog.pojo.ArticleWithAuthorNameVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ArticleMapper {
    /**
     * 查询站内全部文章
     * @return 文章视图列表
     */
    @Select("select a.*, u.username as authorName from article a join user u on a.author_id = u.id " +
            "order by a.created desc")
    List<ArticleWithAuthorNameVO> listAll();

    /**
     * 查询单篇文章
     * @param id 文章id
     * @return 文章视图列表
     */
    @Select("select a.*, u.username as authorName from article a join user u on a.author_id = u.id " +
            "where a.id = #{id}")
    ArticleWithAuthorNameVO listById(Integer id);

    /**
     * 分页查询标签内所有文章
     * @param tagId 标签id
     * @return 文章视图列表
     */
    @Select("select a.*, u.username as authorName from article a join user u on a.author_id = u.id " +
            "join article_tag at on a.id = at.article_id " +
            "where at.tag_id = #{tagId} " +
            "order by a.created desc")
    List<ArticleWithAuthorNameVO> listByTag(Integer tagId);

    /**
     * 分页查看关注的所有文章
     * @param id 当前登录用户id
     * @return 文章视图列表
     */
    @Select("select a.*, u.username as authorName from article a join user u on a.author_id = u.id " +
            "join follow f on u.id = f.user_id " +
            "where f.follower_id = #{id} " +
            "order by a.created desc")
    List<ArticleWithAuthorNameVO> listByFocus(Integer id);

    /**
     * 分页查看用户的所有文章
     * @param id 用户id
     * @return 文章视图列表
     */
    @Select("select a.*, u.username as authorName from article a join user u on a.author_id = u.id " +
            "where u.id = #{id} " +
            "order by a.created desc")
    List<ArticleWithAuthorNameVO> listByUserId(Integer id);

    /**
     * 分页查看用户感兴趣的的所有文章
     * @param id 用户id
     * @return 文章视图列表
     */
    @Select("select distinct a.*, u.username as authorName from article a join user u on a.author_id = u.id " +
            "join article_tag at on at.article_id = a.id " +
            "join hobby h on h.tag_id = at.tag_id " +
            "where h.user_id = #{id}  " +
            "order by a.created desc")
    List<ArticleWithAuthorNameVO> listByHobbies(Integer id);

    /**
     * 分页查看搜索匹配到的所有文章
     * @param condition 匹配字符串
     * @return 文章视图列表
     */
    @Select("select a.*, u.username as authorName from article a join user u on a.author_id = u.id " +
            "where a.title like concat('%',#{condition},'%') " +
            "or a.content like concat('%',#{condition},'%') " +
            "order by a.created desc")
    List<ArticleWithAuthorNameVO> listByCondition(String condition);

    /**
     * 向文章标中添加文章
     * @param article 文章实体类
     */
    @Insert("insert into article (title, image, content, created, modified, author_id, support) " +
            "values (#{title}, #{image}, #{content}, #{created}, #{modified}, #{authorId}, #{support})")
    void insert(Article article);

    /**
     * 编辑文章
     * @param article 文章实体类
     */
    void update(Article article);

    /**
     * 批量删除文章
     * @param ids 文章id
     */
    void delete(List<Integer> ids);

    /**
     * 根据id查找文章
     * @param id 文章id
     * @return 文章实体
     */
    @Select("select * from article where id = #{id}")
    Article findById(Integer id);

    /**
     * 查询用户是否给文章点过赞
     * @param userId 用户id
     * @param articleId 文章id
     * @return 点赞表中出现过这项的次数
     */
    @Select("select count(*) from support where user_id = #{userId} and article_id = #{articleId}")
    Integer countThisSupport(Integer userId, Integer articleId);

    /**
     * 往点赞表中插入数据
     * @param userId 用户id
     * @param articleId 文章id
     */
    @Insert("insert into support (user_id, article_id) values (#{userId}, #{articleId})")
    void insertSupport(Integer userId, Integer articleId);

    /**
     * 删除一篇文章的所有点赞
     * @param id 文章id
     */
    @Delete("delete from support where article_id = #{id}")
    void deleteSupport(Integer id);

    /**
     * 根据文章id删除评论
     * @param id 文章id
     */
    @Delete("delete from comment where article_id = #{id}")
    void deleteCommentsByArticleId(Integer id);

    /**
     * 根据文章id删除文章标签
     * @param id 文章id
     */
    @Delete("delete from article_tag where article_id = #{id}")
    void deleteTagsByArticleId(Integer id);

    /**
     * 向关联表中加入表项
     * @param articleId 文章id
     * @param tagId 标签id
     */
    @Insert("insert into article_tag (article_id, tag_id) values (#{articleId}, #{tagId})")
    void insertArticleTag(Integer articleId, Integer tagId);

    /**
     * 查找最新发布的文章
     * @return 文章实体
     */
    @Select("select * from article order by created desc limit #{limit}")
    Article newsest(@Param("limit") Integer limit);

    /**
     * 按点赞量排序，选前五
     * @return 文章列表
     */
    @Select("select * from article order by support desc limit #{limit}")
    List<Article> listBySupport(@Param("limit") Integer limit);
}
